package codetop.microsoft.T49;

import java.util.*;

/**
 * @Author: 18362
 * @Create: 2022-08-16 10:59:58 星期二
 */
class Solution {
    public List<List<String>> groupAnagrams(String[] strs) {
        int[] cnt = new int[26];
        Map<String, List<String>> map = new HashMap<>();
        for (String str : strs) {
            Arrays.fill(cnt, 0);
            for (int i=0; i<str.length(); ++i) {
                cnt[str.charAt(i)-'a']++;
            }
            StringBuilder sb = new StringBuilder();
            for (int i=0; i<26; ++i) {
                if (cnt[i] > 0) {
                    sb.append(cnt[i]).append((char) (i + 'a'));
                }
            }
            String s = sb.toString();
            if (!map.containsKey(s)) {
                map.put(s, new ArrayList<>());
            }
            map.get(s).add(str);
        }
        return new ArrayList<>(map.values());
    }
}